<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose"
    :append-to-body="true"
  >
    <el-row>
      <el-form
        label-width="80px"
        ref="createForm"
        :rules="rules"
        :model="formData"
      >
        <el-form-item label="菜单名称" prop="title">
          <el-input v-model="formData.title" />
        </el-form-item>
        <el-form-item label="菜单路径" prop="path">
          <el-input v-model="formData.path" />
        </el-form-item>
        <el-form-item label="菜单图标" prop="icon">
          <el-input v-model="formData.icon" disabled>
            <template slot="append">
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="showIcon()"
              ></el-button>
            </template>
          </el-input>
        </el-form-item>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose()">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="comit()"
        >确 定</el-button
      >
    </span>
    <iconModel ref="iconModel" @fromvisible="getIcon"></iconModel>
  </el-dialog>
</template>
<script>
import { CreateOrUpdate } from '@api/systemManage/menuManage'
import iconModel from '@/pages/icon'
export default {
  components: { iconModel },
  data () {
    return {
      title: '',
      dialogVisible: false,
      loading: false,
      formData: {
        title: '',
        path: '',
        icon: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' }
        ],
        path: [
          { required: true, message: '请输入菜单路径', trigger: 'blur' }
        ],
        icon: [
          { required: true, message: '请输入菜单图标', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    showIcon () {
      this.$refs.iconModel.show()
    },
    getIcon (value) {
      this.formData.icon = value
    },
    comit () {
      this.$refs.createForm.validate((valid) => {
        if (valid) {
          CreateOrUpdate(this.formData).then(res => {
            if (res.success) {
              this.handleClose()
              this.$notify({
                title: '提示',
                message: '添加成功',
                type: 'success'
              })
            }
          })
        }
      })
    },
    show (value) {
      this.formData = value
      this.title = '父级菜单:' + this.formData.parentTitle

      this.dialogVisible = true
    },
    handleClose () {
      this.formData = {
        title: '',
        path: '',
        icon: ''
      }
      this.dialogVisible = false
      this.$emit('fromvisible', false)
    }
  }
}
</script>
